﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.IO;

namespace mpsalaryprojectgroup
{
    public partial class NEWUserExportFile : System.Web.UI.Page
    {
        String username = "";
        String loginName = "";
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Session["Usename"] != null)
            {
                username = (string)(Session["Usename"]);
                if (username == null)
                {
                    Response.Redirect("NEWMainPage.aspx");
                }
                else
                {
                    lblUsername.Text = username;
                }
            }
            else if (Session["UsenameLogin"] != null)
            {
                loginName = (string)(Session["UsenameLogin"]);
                if (loginName == null)
                {
                    Response.Redirect("NEWMainPage.aspx");
                }
                else
                {
                    lblUsername.Text = loginName;
                }
            }
            else
            {
                Response.Redirect("NEWMainPage.aspx");
            }
            DisableBufferingOnPage();
        }
        protected void DisableBufferingOnPage()
        {
            Response.Buffer = true;
            Response.ExpiresAbsolute = DateTime.Now.AddDays(-1);
            // set expiry date in the past
            Response.Expires = -1;
            Response.CacheControl = "no-cache";
            Response.Cache.SetNoStore();
            Response.AppendHeader("Pragma", "no-cache");
            Response.AppendHeader("Cache-Control", "no-cache");
            Response.CacheControl = "no-cache";
            Response.Expires = -1;
            Response.ExpiresAbsolute = new DateTime(1900, 1, 1);
            Response.Cache.SetCacheability(HttpCacheability.NoCache);
        }



        private void ExportGridView()
        {
            string attachment = "attachment; filename=LatestResult_2010.xls";
            Response.ClearContent();
            Response.AddHeader("content-disposition", attachment);
            Response.ContentType = "application/ms-excel";
            StringWriter sw = new StringWriter();
            HtmlTextWriter htw = new HtmlTextWriter(sw);
            GridViewExcel0.RenderControl(htw);
            Response.Write(sw.ToString());
            Response.End();
        }

        public override void VerifyRenderingInServerForm(Control control)
        {
        }

        private void PrepareGridViewForExport(Control gv)
        {
            LinkButton lb = new LinkButton();
            Literal l = new Literal();
            string name = String.Empty;
            for (int i = 0; i < gv.Controls.Count; i++)
            {
                if (gv.Controls[i].GetType() == typeof(LinkButton))
                {
                    l.Text = (gv.Controls[i] as LinkButton).Text;
                    gv.Controls.Remove(gv.Controls[i]);
                    gv.Controls.AddAt(i, l);
                }
                else if (gv.Controls[i].GetType() == typeof(DropDownList))
                {
                    l.Text = (gv.Controls[i] as DropDownList).SelectedItem.Text;
                    gv.Controls.Remove(gv.Controls[i]);
                    gv.Controls.AddAt(i, l);
                }
                else if (gv.Controls[i].GetType() == typeof(CheckBox))
                {
                    l.Text = (gv.Controls[i] as CheckBox).Checked ? "True" : "False";
                    gv.Controls.Remove(gv.Controls[i]);
                    gv.Controls.AddAt(i, l);
                }
                if (gv.Controls[i].HasControls())
                {
                    PrepareGridViewForExport(gv.Controls[i]);
                }
            }
        }

        protected void ImageButton1_Click1(object sender, ImageClickEventArgs e)
        {
            //Export the GridView to Excel
            PrepareGridViewForExport(GridViewExcel0);
            ExportGridView();
        }


    }
}